class NovationsController < ApplicationController
  include ControllerHelper
  before_filter :init, :only => [:index, :show]
  before_filter :require_moderator, :except => [:index, :show]
  def new
    set_form_settings
    @page["name"] = "novation"
    @novation = Novation.new
    render :layout => "form"
  end

  def create
    begin
      @novation = Novation.new(params[:novation])
      if @novation.save
        redirect_to novation_moderator_path(:status => "all") and return
      else
        set_form_settings
        @page["name"] = "novation"
        render :action => "new", :layout => "form"
      end
    rescue
      flash[:notice] = I18n.t "site.message.corrupted_html"
      set_form_settings
      @page["name"] = "novation"
      render :action => "new", :layout => "form"
    end
  end

  def moderator
    per_page = params[:per_page] ||= @site["per_page"]["moderator"]["novations"]
    conditions = []
    conditions = merge_conditions(conditions, ["status = ?", "approved"]) if params[:status] == "approved"
    conditions = merge_conditions(conditions, ["status <> ?", "approved"]) if params[:status] == "banned"
    @novations = Novation.find(:all, :conditions => conditions).uniq.paginate(:page => params[:page], :per_page => per_page)
    set_moderator_settings
    render :layout => "cms"
  end

  def destroy
    @novations = Novation.find(:first, :conditions => ["id = ?", params[:id]])
    if @novations
      @novations.destroy
    else
      flash[:notice] = I18n.t "site.message.no_novation"
    end
    redirect_to novation_moderator_path(:status => "all")
  end

  def edit
    set_form_settings
    @page["name"] = "novation"
    @novation = Novation.find(:first, :conditions => ["id = ?", params[:id]])
    if @novation.nil?
      flash[:notice] = I18n.t "site.message.no_novation"
      redirect_to novation_moderator_path(:status => "all")
    else
      render :layout => "form"
    end
  end

  def update
    @novation = Novation.find(:first, :conditions => ["id = ?", params[:id]])
    if @novation.nil?
      flash[:notice] = I18n.t "site.message.no_novation"
      redirect_to novation_moderator_path(:status => "all")
    else
      if @novation.update_attributes(params[:novation])
        redirect_to novation_moderator_path(:status => "all") and return
      else
        set_form_settings
        @page["name"] = "novation"
        render :layout => "form", :action => "edit", :id => params[:id]
      end
    end
  end

  def approve_ban
    novation = Novation.find(params[:id])
    old_status = novation.status == "approved" ? "approved" : "banned"
    novation.status = novation.status == "approved" ? "banned" : "approved"
    novation.save
    redirect_to novation_moderator_path(:status => old_status)
  end

  def index
    per_page = params[:per_page] ||= @site["per_page"]["entity"]["novations"]
    @novations = Novation.find(:all, :conditions => ["status = ?", "approved"]).uniq.paginate(:page => params[:page], :per_page => per_page)
    @page["bonus_title"] = @page["bonus_description"] = @novations.map do |item| item.title end.to_sentence
    @page["bonus_keywords"] = @novations.map do |item| item.title.gsub(" ", ",") end.join(",")
  end

  def show
    @novation = Novation.find(:first, :conditions => ["id = ?", params[:id]])
    if @novation.nil?
      flash[:notice] = I18n.t "site.message.no_receipt"
      redirect_to receipts_path and return
    end
    @page["bonus_title"] = @page["bonus_description"] = @novation.title
    @page["bonus_keywords"] = @novation.title.gsub(" ", ",")
  end

end
